//
//  MainTabBarController.m
//  Project1
//
//  Created by 黄珂耀 on 15/7/17.
//  Copyright (c) 2015年 黄珂耀. All rights reserved.
//

#import "MainTabBarController.h"
#import "Movie.h"
#import "News.h"
#import "TOP.h"
#import "Cinema.h"
#import "More.h"
#import "TabBarButton.h"
#import "BaseNavigationController.h"

#define KWidth [UIScreen mainScreen].bounds.size.width
#define KHeight [UIScreen mainScreen].bounds.size.height

@interface MainTabBarController ()
{
    NSMutableArray *_navArray;
    
//    BOOL isHidden;
}

@end

@implementation MainTabBarController

#pragma mark - DefaultOperation

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.tabBar setTranslucent:YES];
//    self.edgesForExtendedLayout = UIRectEdgeBottom;
    
    [self _createSubviews];
    [self _setTabBar];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - CreateSubviews

- (void)_createSubviews
{
    Movie *movieViewController = [[Movie alloc] init];
    News *newsViewController = [[News alloc] init];
    TOP *topViewController = [[TOP alloc] init];
    Cinema *cinemaViewController = [[Cinema alloc] init];
    More *moreViewController = [[More alloc] init];
    
    NSArray *vcArray = @[movieViewController,newsViewController,topViewController,cinemaViewController,moreViewController];
    
    _navArray = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < 5; i++)
    {
        BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:vcArray[i]];
        [_navArray addObject:nav];
    }
    
    self.viewControllers = _navArray;
    
}

#pragma mark - SetTabBar

- (void)_setTabBar
{
    for (UIView *subView in self.tabBar.subviews)
    {
        Class cls = NSClassFromString(@"UITabBarButton");
        
        if ([subView isKindOfClass:cls])
        {
            [subView removeFromSuperview];
        }
    }
    
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_bg_all"]];
    
    NSArray *titleNames = @[@"电影",@"新闻",@"TOP",@"影院",@"更多"];
    NSArray *imageNames = @[@"movie_home@2x",@"msg_new@2x",@"start_top250@2x",@"icon_cinema@2x",@"more_setting@2x"];

//    for (int i = 0; i < 5; i++)
//    {
//        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(KWidth / 5 * i, 0, KWidth / 5, 49)];
//        [button setTitle:titleNames[i] forState:UIControlStateNormal];
//        button.titleLabel.font = [UIFont systemFontOfSize:11];
//        [button setImage:[UIImage imageNamed:imageNames[i]] forState:UIControlStateNormal];
//        
////        button .titleEdgeInsets = UIEdgeInsetsMake(30, -18, 0, 0);
////        button.imageEdgeInsets = UIEdgeInsetsMake(-10, -20, 0, 0);
//        
//        [self.tabBar addSubview:button];
//    }
    
    _shadowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KWidth / 5, 49)];
    [_shadowImageView setImage:[UIImage imageNamed:@"selectTabbar_bg_all1"]];
    
    [self.tabBar addSubview:_shadowImageView];
    
    for (int i = 0; i < 5; i++)
    {
        TabBarButton *button = [[TabBarButton alloc] initWithFrame:CGRectMake(i * KWidth / 5, 0, KWidth / 5, 49) andImageName:imageNames[i] andTitle:titleNames[i]];
        button.tag = i;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.tabBar addSubview:button];
    }

}

#pragma mark - Actions

- (void)buttonAction:(TabBarButton *)tabBarButton
{
    
//    NSLog(@"%@",tabBarButton.label.text);
    NSInteger index = tabBarButton.tag;
    self.selectedIndex = index;
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.3];
    
    _shadowImageView.center = tabBarButton.center;
    
    [UIView commitAnimations];
    
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
